package com.maiworld.content.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.content.service.SubjectLService;
import com.maiworld.mapper.SubjectLClMapper;
import com.maiworld.pojo.Subject;

import entity.PageResult;

@Service
public class SubjectLServiceImpl implements SubjectLService {

	@Autowired
	private SubjectLClMapper subjectLClMapper;
	@Override
	public int resetSubjectL(List<Subject> subjects) {
		//清空单选表
		subjectLClMapper.truncateSingleTable();
		//清空判断表
		subjectLClMapper.truncateJudgeTable();
		//清空多选表
		subjectLClMapper.truncateMultipleTable();
		
		List<Subject> single = new ArrayList<Subject>();
		
		List<Subject> judge = new ArrayList<Subject>();;
		
		List<Subject> multiple = new ArrayList<Subject>();
		
		for(Subject sub:subjects) {
			if(Integer.parseInt(sub.getAnswer()) > 4) {
				multiple.add(sub);
			}else {
				if(sub.getItem3().length() != 0 && sub.getItem4().length() != 0) {		
					 single.add(sub);
				}else {
					sub.setItem1("正确");
					sub.setItem2("错误");
					judge.add(sub);
				}
			}
		}

		if(multiple.size() > 0) {
			//向多选表添加数据
			subjectLClMapper.insertMultiple(multiple);
		}
		
		if(single.size() > 0) {
			//向单选表添加数据
			subjectLClMapper.insertSingle(single);
		}
		
		if(judge.size() > 0) {
			//向判断表中添加数据
			subjectLClMapper.insertJudge(judge);
		}
		
		return 1;
	}
	@Override
	public PageResult selectBySubject(Subject subject, int page, int rows) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, rows);
		Page<Subject> pageList = new Page<Subject>();
		if(Integer.parseInt(subject.getTopicTypes()) == 1) {
			 pageList = (Page<Subject>)subjectLClMapper.selectSingleBySubject(subject);
		}
		
		if(Integer.parseInt(subject.getTopicTypes()) == 2) {
			 pageList = (Page<Subject>)subjectLClMapper.selectJudgeBySubject(subject);
		}
		
		if(Integer.parseInt(subject.getTopicTypes()) == 3) {
			 pageList = (Page<Subject>)subjectLClMapper.selectMultipleBySubject(subject);
		}
		
		
		return new PageResult(pageList.getTotal(), pageList.getResult());
	}
	@Override
	public int saveSujectLCl(Subject subject) {
		// TODO Auto-generated method stub
		int res = 0;
		if(Integer.parseInt(subject.getTopicTypes()) == 1) {
			res = subjectLClMapper.saveSingleBySubject(subject);
		}
		if(Integer.parseInt(subject.getTopicTypes()) == 2) {
			res = subjectLClMapper.saveJudgeBySubject(subject);
		}
		if(Integer.parseInt(subject.getTopicTypes()) == 3) {
			res = subjectLClMapper.saveMultipleBySubject(subject);
		}
		return res;
	}
	@Override
	public Map<String,Integer> countSub1Number() {
		// TODO Auto-generated method stub
		Map<String,Integer> res = new HashMap<String,Integer>();
		int single = subjectLClMapper.countSingle();
		int judge = subjectLClMapper.countJudge();
		res.put("singleNum", single);
		res.put("judgeNum", judge);
		res.put("countNum", judge + single);
		return res;
	}
	
	/**
	 * 随机获取一套题 100道  40道判断  60道单选
	 */
	@Override
	public List<Subject> randTest() {
		// TODO Auto-generated method stub
		int single = subjectLClMapper.countSingle();
		int judge = subjectLClMapper.countJudge();
		ArrayList<Integer> listJudge = getRandArray(judge,40);
		Long[] arrJudge = new Long[40];
		for (int i = 0; i < listJudge.size(); i++) {//最终将结果展示在控制台上
			arrJudge[i] = Long.parseLong(listJudge.get(i).toString());
		}
		List<Subject> list = subjectLClMapper.selectJudgeByArray(arrJudge);
		
		ArrayList<Integer> listSingle = getRandArray(single,60);
		Long[] arrSingle = new Long[60];
		for (int i = 0; i < listSingle.size(); i++) {//最终将结果展示在控制台上
			arrSingle[i] = Long.parseLong(listSingle.get(i).toString());
		}
		List<Subject> list2 = subjectLClMapper.selectSingleByArray(arrSingle);
		list.addAll(list2);
		return list;
	}
	/**
	 * 获取随机数数组
	 * @param count
	 * @param num
	 * @return
	 */
	public ArrayList<Integer> getRandArray(int count,int num){
		Set<Object> set = new HashSet<>();
		ArrayList<Integer> list   = new ArrayList<Integer>();
		while(true) {
			int 	radom = (int)(Math.random()*(count) + 1);
			set.add(radom);
			if(set.size()==num) {
				break;
			}		
		}
		for (Object int1 : set) {
			list.add((Integer) int1);
		}
		return list;
	}
	/**
	 * 获取顺序模拟题 100道 40判断 60单选
	 */
	@Override
	public List<Subject> orderTest(int page) {
		// TODO Auto-generated method stub
		int single = subjectLClMapper.countSingle();
		int judge = subjectLClMapper.countJudge();
		
		double a = single;		
		int maxSingle = (int)Math.ceil(a/60);

		int pagesingle = page%maxSingle == 0?maxSingle:page%maxSingle;
		int singleStart = (pagesingle -1)*60;
		int singleSize = 60;
		List<Subject> list2 = subjectLClMapper.selectSingleBypage(singleStart, singleSize);
		
		double b = judge;		
		int maxJudge = (int)Math.ceil(b/40);

		int pagejudge = page%maxJudge == 0?maxJudge:page%maxJudge;
		int judgeStart = (pagejudge -1)*40;
		int judgeSize = 40;
		List<Subject> list = subjectLClMapper.selectJudgeBypage(judgeStart, judgeSize);
		list.addAll(list2);
		return list;
	}
	@Override
	public List<Subject> orderSingle(int page, int pageSize) {
		// TODO Auto-generated method stub
		int singleStart = (page -1)*pageSize;
		int singleSize = pageSize;
		List<Subject> list2 = subjectLClMapper.selectSingleBypage(singleStart, singleSize);
		return list2;
	}
	@Override
	public List<Subject> orderJudge(int page, int pageSize) {
		// TODO Auto-generated method stub
		int judgeStart = (page -1)*pageSize;
		int judgeSize = pageSize;
		List<Subject> list = subjectLClMapper.selectJudgeBypage(judgeStart, judgeSize);
		return list;
	}

}
